package com.d2rv.guide;

import com.d2rv.offscreen.OffScreen2D;
import controlP5.Accordion;
import controlP5.ControlP5;
import controlP5.Group;
import controlP5.RadioButton;
import controlP5.Slider;
import java.io.File;
import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;

/**
 * This class creates a Graphical User Interface for the guide collaboration.
 *
 * @author Darrieutort A., Dupouy A., Rossignol L., Van Poucke A.
 */
public class GuideGUI {

    private ControlP5 cp5;
    private Accordion accordion;
    private RadioButton rtools, r3d, rht, rlight;
    private Slider zoom;

    /**
     * Default constructor.
     *
     * @param parent the current application
     * @param offscreen the current offscreen
     */
    public GuideGUI(PApplet parent, OffScreen2D offscreen) {
        this.cp5 = new ControlP5(parent);
        setupGUI(parent, offscreen);
    }

    /**
     * Draw the GUI on the screen.
     */
    public void draw() {
        this.cp5.draw();
    }

    /**
     * Creates the GUI.
     *
     * @param parent the current application
     * @param offscreen the current offscreen
     */
    private void setupGUI(PApplet parent, OffScreen2D offscreen) {
        this.cp5.setAutoDraw(false);

        PFont pf = parent.createFont("arial", 12, false);
        PFont pf2 = parent.createFont("Arial", 18, true);
        this.cp5.setFont(pf);

        Group tools = this.cp5.addGroup("Tools").setBackgroundColor(parent.color(0, 64)).setBackgroundHeight(offscreen.height / 3).hideBar();
        Group modes = this.cp5.addGroup("Modes").setBackgroundColor(parent.color(0, 64)).setBackgroundHeight(offscreen.height / 6).hideBar();
        Group instructions = this.cp5.addGroup("Instructions").setBackgroundColor(parent.color(0, 64)).setBackgroundHeight(offscreen.height / 4 - 20).hideBar();
        
        PImage arrowimageleft = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Left.png"));
        PImage arrowimageright = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Right.png"));
        PImage arrowimageup = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Up.png"));
        PImage arrowimagedown = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Down.png"));

        PImage arrowimageleftred = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Left-Red.png"));
        PImage arrowimagerightred = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Right-Red.png"));
        PImage arrowimageupred = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Up-Red.png"));
        PImage arrowimagedownred = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Down-Red.png"));

        PImage arrowimageleftgrey = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Left-Grey.png"));
        PImage arrowimagerightgrey = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Right-Grey.png"));
        PImage arrowimageupgrey = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Up-Grey.png"));
        PImage arrowimagedowngrey = parent.loadImage(parent.dataPath("pictures" + File.separator + "Arrow-Down-Grey.png"));

        PImage logo = parent.loadImage(parent.dataPath("pictures" + File.separator + "logo.png"));
        PImage logo2 = parent.loadImage(parent.dataPath("pictures" + File.separator + "partenaire.png"));

        int imageHeight = arrowimageleft.height;

        this.cp5.addButton("logo").setImage(logo).updateSize().setMoveable(false).setPosition(offscreen.width / 2 - logo.width / 2, 10);

        int modesHeight = modes.getHeight();

        this.cp5.addTextlabel("labelmode").setText("Modes").setPosition((offscreen.width / 2) - (imageHeight / 2) - 25, modesHeight).setColor(0).setFont(pf2).moveTo(modes);

        r3d = this.cp5.addRadioButton("radioModeFunction").setPosition(10, 40).setItemWidth(30).setItemHeight(30).addItem("Destination", 1).addItem("Course", 2).setColorLabel(parent.color(255)).activate(2).moveTo(modes).setSpacingRow(10);

        int instructionWidth = instructions.getWidth();
        int instructionHeight = instructions.getHeight();

        this.cp5.addTextlabel("instructionslabel").setText("Instructions").setPosition((offscreen.width / 2) - (imageHeight / 2) - 37, modesHeight).setColor(0).setFont(pf2).moveTo(instructions);

        this.cp5.addButton("arrowLeft").setPosition((offscreen.width / 2) - (imageHeight / 2) - 20 - (imageHeight), instructionHeight * 5 + imageHeight + 15).setImages(arrowimageleft, arrowimageleftgrey, arrowimageleftred).updateSize().setMoveable(false).moveTo(instructions);

        this.cp5.addButton("arrowRight").setPosition((offscreen.width / 2) - (imageHeight / 2) - 10 + (imageHeight), instructionHeight * 5 + imageHeight + 15).setImages(arrowimageright, arrowimagerightgrey, arrowimagerightred).updateSize().setMoveable(false).moveTo(instructions);

        this.cp5.addButton("arrowUp").setPosition((offscreen.width / 2) - (imageHeight / 2) - 15, instructionHeight * 5 + 10).setImages(arrowimageup, arrowimageupgrey, arrowimageupred).updateSize().moveTo(instructions).setMoveable(false);

        this.cp5.addButton("arrowDown").setPosition((offscreen.width / 2) - (imageHeight / 2) - 15, instructionHeight * 5 + imageHeight + 15).setImages(arrowimagedown, arrowimagedowngrey, arrowimagedownred).updateSize().moveTo(instructions).setMoveable(false);


        int toolsheightback = tools.getBackgroundHeight();

        this.cp5.addTextlabel("toolslabel").setText("Tools").setPosition((offscreen.width / 2) - (imageHeight / 2) - 22, modesHeight).setColor(0).setFont(pf2).moveTo(tools);

        zoom = this.cp5.addSlider("zoom").setBroadcast(false).setRange(50, 5).setPosition(offscreen.width - 80, 30).setSize(20, (toolsheightback - 60)).setBroadcast(true).setMoveable(false).moveTo(tools).setColorValueLabel(0);

        this.cp5.addTextlabel("3D").setText("Stereo mode").setPosition((offscreen.width / 10 - 25), modesHeight + 25).setColor(0).setFont(parent.createFont("Arial", 16)).moveTo(tools);

        rtools = this.cp5.addRadioButton("radioToolsButton").setPosition(10, 60).setItemWidth(30).setItemHeight(30).addItem("Anaglyphe", 1).addItem("Side by Side", 2).setColorLabel(parent.color(255)).activate(2).moveTo(tools).setSpacingRow(5);
        
        rht = this.cp5.addRadioButton("radioHeadTrackingButton").setPosition(10, 145).setItemWidth(30).setItemHeight(30).addItem("Head-tracking", 1).setColorLabel(parent.color(255)).moveTo(tools);
        
        rlight = this.cp5.addRadioButton("radioLightButton").setPosition(10, 200).setItemWidth(30).setItemHeight(30).addItem("Lights", 1).setColorLabel(parent.color(255)).activate(1).moveTo(tools).activate(0);

        this.cp5.addButton("logo2").setImage(logo2).updateSize().setMoveable(false).setPosition(offscreen.width / 2 - logo2.width / 2, offscreen.height - logo2.height);

        this.accordion = this.cp5.addAccordion("acc").setPosition(10, 90).setWidth(offscreen.width - 20).addItem(tools).addItem(modes).addItem(instructions);

        this.accordion.open(0, 1, 2);

        this.accordion.setCollapseMode(Accordion.MULTI);
    }
    
    /**
     * Resets the GUI.
     */
    public void reset(){
        rlight.activate(0);
        rtools.deactivateAll();
        r3d.deactivateAll();
        rht.deactivateAll();
        zoom.setValue(5);
    }
}